package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.TeacherIcardInfo;

public class TeacherIcardInfoRowMapper implements RowMapper<TeacherIcardInfo> {

    private int type;

    public static final int AND_TEACHERNAME = 1;
    
    public TeacherIcardInfoRowMapper() {
    }

    public TeacherIcardInfoRowMapper(int type) {
        this.type = type;
    }
    
    @Override
    public TeacherIcardInfo mapRow(ResultSet rs, int rowNum)
        throws SQLException {
        TeacherIcardInfo tii = null;
        switch (type) {
            case AND_TEACHERNAME:
                tii = parseTeacherIcardInfo(new TeacherIcardInfo(), rs, "tii");
                tii.setTeacherName(rs.getString("teacherName"));
                
                if((short)1==tii.getType()){
                    tii.setType_str("进校园");
                }else{
                    tii.setType_str("出校园");
                }    
                
                tii.setCdate_str(SystemDateFormatUtil.formatDate(tii.getCdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                break;
            default:
                tii = parseTeacherIcardInfo(new TeacherIcardInfo(), rs);
        }
        return tii;
    }

    public static TeacherIcardInfo parseTeacherIcardInfo(TeacherIcardInfo tii,
            ResultSet rs) throws SQLException {
        return parseTeacherIcardInfo(tii, rs, null);
    }

    public static TeacherIcardInfo parseTeacherIcardInfo(TeacherIcardInfo tii,
            ResultSet rs, String su) throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            tii.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            tii.setCdate(null);
        }
        try {
            tii.setIcid(rs.getLong(su + "icid"));
        } catch (SQLException e) {
            tii.setIcid(null);
        }
        try {
            tii.setTid(rs.getLong(su + "tid"));
        } catch (SQLException e) {
            tii.setTid(null);
        }
        try {
            tii.setTiid(rs.getLong(su + "tiid"));
        } catch (SQLException e) {
            tii.setTiid(null);
        }
        try {
            tii.setType(rs.getShort(su + "type"));
        } catch (SQLException e) {
            tii.setType(null);
        }
        try {
            tii.setTimetype(rs.getShort(su + "timetype"));
        } catch (SQLException e) {
            tii.setTimetype(null);
        }
        try {
            tii.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            tii.setImg(null);
        }
        
        return tii;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            TeacherIcardInfo tii) {
        par.put("cdate", tii.getCdate());
        par.put("icid", tii.getIcid());
        par.put("tid", tii.getTid());
        par.put("tiid", tii.getTiid());
        par.put("type", tii.getType());
        par.put("timetype", tii.getTimetype());
        par.put("img", tii.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            TeacherIcardInfo tii) {
        args.put("tiid", tii.getTiid());
        return args;
    }
    
    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final TeacherIcardInfo tii) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(tii),
                        new String[] { "tiid" });
                parsePS(ps, tii);
                return ps;
            }
        };
    }

    private static String parseSQL(TeacherIcardInfo tii) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into teacher_icard_info(");
        int count = 0;
        if (null != tii.getIcid()) {
            sql.append("icid,");
            count++;
        }

        if (null != tii.getTid()) {
            sql.append("tid,");
            count++;
        }

        if (null != tii.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != tii.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null != tii.getTimetype()) {
            sql.append("timetype,");
            count++;
        }
        if (null != tii.getImg()) {
            sql.append("img,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, TeacherIcardInfo tii)
        throws SQLException {
        int count = 1;
        if (null != tii.getIcid()) {
            ps.setLong(count++,tii.getIcid());
        }
        
        if (null != tii.getTid()) {
            ps.setLong(count++, tii.getTid());
        }

        if (null != tii.getType()) {
            ps.setShort(count++, tii.getType());
        }
        
        if (null != tii.getCdate()) {
            ps.setTimestamp(count++, tii.getCdate());
        }

        if (null != tii.getTimetype()) {
            ps.setShort(count++, tii.getTimetype());
        }
        
        if (null != tii.getImg()) {
            ps.setString(count++, tii.getImg());
        }
    }
}